package com.wg.msshopsp.backendmanagement.shop.controller;

import com.wg.msshopsp.backendmanagement.shop.entity.Product;
import com.wg.msshopsp.backendmanagement.shop.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    // 商品上架接口
    @PostMapping("/shop/product/upload")
    public Map<String, Object> uploadProduct(@RequestBody Product product) {
        // 直接调用服务层（若抛出异常，会被全局异常处理器捕获）
        productService.uploadProduct(product);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "商品上架成功");
        response.put("data", product.getId());
        return response;
    }

    // 商品更新接口
    @PutMapping("/shop/product/update")
    @Operation(summary = "更新商品信息", description = "支持更新商品名称、价格、库存、状态、规格等信息")
    public Map<String, Object> updateProduct(
            @Parameter(description = "商品完整信息（必须包含id）", required = true)
            @RequestBody Product product
    ) {
        productService.updateProduct(product);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "商品更新成功");
        response.put("data", product.getId());
        return response;
    }


}
